﻿
Imports System.Runtime.CompilerServices
Imports System.Net.Sockets
Imports osi.root.procedure
Imports osi.root.formation
Imports osi.root.utils
Imports osi.service.tcp.constants

Public Module _question
    <Extension()> Public Function question(ByVal client As TcpClient,
                                           ByVal sd() As Byte,
                                           ByVal rd As pointer(Of Byte()),
                                           Optional ByVal send_rate_second As Int32 = _
                                                                default_value.send_rate_sec,
                                           Optional ByVal response_timeout_ms As Int64 = _
                                                                default_value.response_timeout_ms,
                                           Optional ByVal receive_rate_second As Int32 = _
                                                                default_value.receive_rate_sec) As event_comb
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  ec = package_send(client, sd, send_rate_second)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  If ec.end_result() Then
                                      ec = package_receive(client, rd, response_timeout_ms, receive_rate_second)
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  Else
                                      Return False
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function question(ByVal client As TcpClient,
                                           ByVal p As powerpoint,
                                           ByVal sd() As Byte,
                                           ByVal rd As pointer(Of Byte())) As event_comb
        Return If(p Is Nothing,
                  question(client,
                           sd,
                           rd),
                  question(client,
                           sd,
                           rd,
                           p.send_rate_sec,
                           p.response_timeout_ms,
                           p.receive_rate_sec))
    End Function

    <Extension()> Public Function question(ByVal p As powerpoint,
                                           ByVal sd() As Byte,
                                           ByVal rd As pointer(Of Byte())) As event_comb
        Dim c As TcpClient = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  If p Is Nothing OrElse Not p.get(c) Then
                                      Return False
                                  Else
                                      ec = question(c, p, sd, rd)
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  End If
                              End Function,
                              Function() As Boolean
                                  assert(Not p Is Nothing)
                                  p.release(c)
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function

    Public Function question(ByVal name As String,
                             ByVal sd() As Byte,
                             ByVal rd As pointer(Of Byte())) As event_comb
        Dim p As powerpoint = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  If connection_manager.get(name, p) Then
                                      ec = question(p, sd, rd)
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  Else
                                      Return False
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function
End Module
